from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from pprint import pprint
from selenium.webdriver.support.wait import WebDriverWait

# 打开浏览器，访问qq邮箱网页
driver = webdriver.Chrome()
driver.get('https://mail.163.com/')


# 登录163邮箱
# 获取
# 切换到登录框架
f1 = driver.find_element(By.TAG_NAME, 'iframe')
driver.switch_to.frame(f1)
# self.driver.switch_to.frame('normalLoginTab')
# 输入账号密码，并点击登录
driver.find_element(By.NAME, 'email').send_keys('......')
driver.find_element(By.NAME, 'password').send_keys('....')
# time.sleep(10)
driver.find_element(By.ID, 'dologin').click()

# 定位写信元素，并点击
driver.implicitly_wait(30)  # 等待页面加载时长
driver.find_element(By.XPATH, '//li[@id="_mail_component_149_149"]').click()
# 输入收件人邮箱
f1= driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt')
f1.send_keys('1148691054@qq.com')
# 输入主题
f2 = driver.find_element(By.XPATH, '//input[contains(@id, "_subjectInput")]')
f2.send_keys('test')
# 切换框架, 并写入正文
f3 = driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
driver.switch_to.frame(f3)
# 定位正文
p1 = driver.find_element(By.TAG_NAME, 'body')
p1.click()
p1.send_keys('test')
# 定位发送按钮，并点击
driver.switch_to.parent_frame()
s1 = driver.find_element(By.TAG_NAME, 'footer')
s1.find_element(By.TAG_NAME, 'div').click()


# 退出浏览器
time.sleep(10)
driver.quit()
